package com.custom.forcedark;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mBtn;
    private Boolean mUseMyTheme;

    @SuppressLint("ObsoleteSdkInt")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        super.onCreate(savedInstanceState);
        final SharedPreferences sharedPreferences = getSharedPreferences("useMyTheme", MODE_PRIVATE);//获取主题更换状态
        mUseMyTheme = sharedPreferences.getBoolean("useMyTheme", true);//第一次默认设置为亮色主题
        if (!mUseMyTheme) {
            setTheme(R.style.NightTheme);//黑色主题
        } else {
            setTheme(R.style.LightTheme);//亮色主题
        }
        setContentView(R.layout.activity_main);
        mBtn = findViewById(R.id.btn_setTheme);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUseMyTheme = !mUseMyTheme;//切换主题状态
                recreate();//重启资源
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("useMyTheme", mUseMyTheme);//存储主题更换状态
                editor.commit();

            }
        });
    }
}
